<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="单位名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unitId">
              <select-Company-input :disabled="formDisabled || isUnitIdDisabled" v-model="model.unitId" placeholder="请选择单位名称" @back="handleUnitBack" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="公积金基数(元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="baseNum">
              <a-input-number
                :disabled="formDisabled"
                :max="99999999"
                :min="0"
                v-model="model.baseNum"
                placeholder="请输入公积金基数"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="当前公积金单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="currFlag">
              <j-dict-select-tag
                :disabled="formDisabled || model.unitId == '1631552502817947649'"
                type="radio"
                v-model="model.currFlag"
                dictCode="yn"
                placeholder="请选择当前公积金单位"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="缴纳开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beginTime">
              <a-date-picker
                :disabled="formDisabled || model.unitId == '1631552502817947649'"
                placeholder="请选缴纳开始时间"
                valueFormat="YYYY-MM-DD"
                @change="handleChange"
                v-model="model.beginTime"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.currFlag == '0'">
            <a-form-model-item label="缴纳结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <a-date-picker
                :disabled="formDisabled"
                :disabled-date="disabledDate"
                :default-picker-value="this.model.beginTime"
                placeholder="请选择缴纳结束时间"
                valueFormat="YYYY-MM-DD"
                v-model="model.endTime"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea :disabled="formDisabled" :max-length="200" v-model="model.remark" rows="4" placeholder="请输入备注"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { validateBlankValue } from '@/utils/util'
import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput' // 公司单位
import moment from 'moment';
export default {
  name: 'ErpEmployeeProvidentFundUnitInfoForm',
  components: {
    SelectCompanyInput
  },
  props: {
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 }
      },
      confirmLoading: false,
      validatorRules: {
        unitId: [
          { required: true, message: '请选择单位名称!' }
        ],
        baseNum: [
          { required: true, message: '请输入公积金基数!' }
        ],
        currFlag: [
          { required: true, message: '请选择当前公积金单位!' }
        ],
        beginTime: [
          { required: true, message: '请选择缴纳开始时间!' }
        ],
        endTime: [
          { required: true, message: '请选择缴纳结束时间!' }
        ]
      },
      url: {
        add: '/api/erp/files/employee/erpEmployeeFamilySocialInfo/add',
        edit: '/api/erp/files/employee/erpEmployeeFamilySocialInfo/edit',
        queryById: '/api/erp/files/employee/erpEmployeeFamilySocialInfo/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
    // 判断当点击编辑的时候，则不可修改单位名称
    isUnitIdDisabled() {
      return !!this.model.unitId
    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    disabledDate(current) {
      if (this.model.beginTime) {
        return current && current < moment(this.model.beginTime).endOf('day')
      } else {
        return null
      }
    },
    handleChange () {
      this.$set(this.model, 'endTime', '')
    },
    add() {
      this.edit(this.modelDefault);
      this.$set(this.model, 'unitType', 'gjj')
    },
    edit(record) {
      this.$refs.form.clearValidate()
      this.model = Object.assign({}, record);
      if (!this.model.currFlag && this.model.unitId != '1631552502817947649') {
        this.$set(this.model, 'currFlag', '1')
      }
      this.$set(this.model, 'unitType', 'gjj')
      this.visible = true;
    },
    handleUnitBack(val) {
      if (val && val.length) {
        this.$set(this.model, 'unitName', val[0].fileName)
        if (val[0].id == '1631552502817947649') {
          this.$delete(this.validatorRules, 'currFlag')
          this.$delete(this.validatorRules, 'beginTime')
          this.$refs.form.clearValidate(['currFlag', 'beginTime'])
          this.$set(this.model, 'currFlag', '')
          this.$set(this.model, 'beginTime', '')
        } else {
          this.$set(this.validatorRules, 'currFlag', [{ required: true, message: '请选择当前参保单位!' }])
          this.$set(this.validatorRules, 'beginTime', [{ required: true, message: '请选择参保开始时间!' }])
          this.$set(this.model, 'currFlag', '1')
        }
      } else {
        this.$set(this.model, 'unitName', '')
      }
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          // that.confirmLoading = true;
          // let httpurl = '';
          // let method = '';
          // if (!this.model.id) {
          //   httpurl += this.url.add;
          //   method = 'post';
          // } else {
          //   httpurl += this.url.edit;
          //   method = 'put';
          // }
          // httpAction(httpurl, this.model, method).then((res) => {
          //   if (res.success) {
          //     that.$message.success(res.message);
          //     that.$emit('ok');
          //   } else {
          //     that.$message.warning(res.message);
          //   }
          // }).finally(() => {
          //   that.confirmLoading = false;
          // })
          that.$emit('ok', that.model);
        }
      })
    }
  }
}
</script>
